package com.peaker.advertisingpro.view

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.Window

import com.google.gson.Gson
import com.peaker.advertisingpro.R
import com.peaker.advertisingpro.activity.SysSetActivity
import com.peaker.advertisingpro.activity.appinfoActivity
import com.peaker.advertisingpro.utils.*
import kotlinx.android.synthetic.main.settingdialog.*
import java.io.File
import java.text.SimpleDateFormat
import java.util.*


/**
 *  @ 作者 CODE_LYF
 *  @ 时间   2021/3/18.
 *  代码人生
 */
class SetDialog(context: Context) : Dialog(context), View.OnClickListener {

    init {
        window?.requestFeature(Window.FEATURE_NO_TITLE)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settingdialog)
        setCanceledOnTouchOutside(false)
        btn_login_manage.setOnClickListener(this)
        btn_app_set.setOnClickListener(this)
        btn_appinfo.setOnClickListener(this)
        btn_devices_info.setOnClickListener(this)

        tv_ip.text = SmdtManagerUtils.smdtGetEthIPAddress()
        tv_server.text = if (TextUtils.isEmpty(SPUtil.getSP(Common.IP))) Common.ip else SPUtil.getSP(Common.IP)

        tv_name.text =
            if (TextUtils.isEmpty(SPUtil.getSP(Common.MACNAME))) SmdtManagerUtils.smdtGetEthMacAddress() else SPUtil.getSP(
                Common.MACNAME
            )

        tv_login.text = if (TextUtils.isEmpty(SPUtil.getSP(Common.LOGIN_STATE))) "未登录" else SPUtil.getSP(
            Common.LOGIN_STATE
        )

    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.btn_login_manage -> {
                dismiss()
                QiyeDialog(context).show()

            }
            R.id.btn_app_set -> {
                dismiss()
                var intent = Intent(context, SysSetActivity::class.java)
                context.startActivity(intent)
            }
            R.id.btn_appinfo -> {
                var intent = Intent(context, appinfoActivity::class.java)
                context.startActivity(intent)
            }
            R.id.btn_devices_info -> {

                var map = mutableMapOf<String, String>()
                var simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                var nowDate = simpleDateFormat.format(Date())

                var default = TimeZone.getDefault()
                var zoneStr = default.getDisplayName(false, TimeZone.SHORT) + default.id

                map["版本"] = "版本号 " + SmdtManagerUtils.getFormattedKernelVersion()
                map["系统版本"] =
                    "Android/${SmdtManagerUtils.getDeviceBrand()}" + SmdtManagerUtils.getAndroidModel() + "/" + SmdtManagerUtils.getAndroidVersion()
                map["储存空间"] =
                    "内置SD: " + "(可用)${SmdtManagerUtils.getAvailableMemory()}/" + SmdtManagerUtils.getInternalStorageMemory() + "(总共大小)"
                map["时间"] = nowDate
                map["时区"] = zoneStr
                map["亮度"] = "当前亮度: ${SmdtManagerUtils.getScreenBrightness()}   最大亮度: 100"
                map["音量"] = "当前音量: ${SmdtManagerUtils.getCurrentSysVolume()}    最大音量: 100"
                map["mac地址"] = SmdtManagerUtils.smdtGetEthMacAddress()
                var sp = SPUtil.getSP(Common.AUTH, "无")
                map["是否自动开机"] = "$sp"
                var toJson = Gson().toJson(map)
                try {
                var file = File("/mnt/usb_storage/")
//todo 导出到U盘
                var smdtGetUSBPath0 = SmdtManagerUtils.smdtGetUSBPath(0)
                var smdtGetUSBPath1 = SmdtManagerUtils.smdtGetUSBPath(1)
                var smdtGetUSBPath2 = SmdtManagerUtils.smdtGetUSBPath(2)
                var smdtGetUSBPath3 = SmdtManagerUtils.smdtGetUSBPath(3)

                LogToFile.d("导出信息地址: ","$smdtGetUSBPath0 $smdtGetUSBPath1 $smdtGetUSBPath2 $smdtGetUSBPath3")
                Log.d("路径: ", "$smdtGetUSBPath0 $smdtGetUSBPath1 $smdtGetUSBPath1 $smdtGetUSBPath2 $smdtGetUSBPath3")
//                /mnt/usb_storage/USB_DISK0/udisk0 /
                if (file.listFiles().isEmpty()) {
                    ToastUtils.s("请插入U盘")
                    return
                }

                if(File(smdtGetUSBPath0).isDirectory){
                    FileUtils.writeFile("$smdtGetUSBPath0${File.separator}appinfo.json", toJson) {
                        //写入成功
                        ToastUtils.s("导出成功")
                    }
                    return
                }

                if(File(smdtGetUSBPath1).isDirectory){
                    FileUtils.writeFile("$smdtGetUSBPath1${File.separator}appinfo.json", toJson) {
                        //写入成功
                        ToastUtils.s("导出成功")
                    }
                    return
                }



                    var filepath =""
//                    val path = file.listFiles()?.first()?.listFiles()?.first()?.path
                    val first = file.listFiles()?.first()
                    if (first?.isDirectory==true){
                        var first1 = first.listFiles().first()
                        if (first1?.isDirectory==true){
                            filepath = first1.path
                        }
                    }

                    if (filepath.isNullOrEmpty().not()){
                        FileUtils.writeFile("$filepath${File.separator}appinfo.json", toJson) {
                            //写入成功
                            ToastUtils.s("导出成功")
                        }
                    }


                }catch (e :Exception){
                    ToastUtils.s(e.message!!)
                }

            }

        }
    }



}